--[[
  湖南烟草 - 统一订单系统 - HTTP
]]
require("apm_core")
require("ApmXml")
require("ApmJson")
require("ApmHttpUtil")
require("extension")

local G_DEBUG = true

if G_DEBUG == false then
    function logDebug(fmt,...) end
    function logInfo(fmt,...) end
    function logWarn(fmt,...) end
    function logError(fmt,...) end
    function logFatal(fmt,...) end
end

local KRED='\x1B[0;31m'
local KSALMON='\x1B[1;31m'
local KGREEN='\x1B[0;32m'
local KLIMEGREEN='\x1B[1;32m'
local KYELLOW='\x1B[0;33m'
local KORANGE='\x1B[0;34m'
local KBLUE='\x1B[1;34m'
local KMAGENTA='\x1B[0;35m'
local KPINK='\x1B[1;35m'
local KCYAN='\x1B[0;36m'
local RESET='\x1B[0m'

local function parseIp(ip)
    local _, _, value = string.find(ip, "(%d+.%d+)$")
    return value == nil and "" or value
end

function post_analysis()
    local ApmData = require("ApmData")
    if ApmData.getL7Proto() ~= 80 then return end

    -- 删除title的URL中带着的;jsessionid=字段内容，否则会造成APM resource资源暴涨
    local s_title = ApmData.getField("transaction.title", 1)
    local i = s_title:find(";jsessionid")
    if i ~= nil then
        ApmData.replaceField("transaction.title", s_title:sub(1, i - 1))
    end

    if ApmData.getMsgType() ~= 1 then return end

--    logInfo("HTTP:["..KGREEN.."%s"..RESET.."]", ApmData.dumpFields(true))
    local s_transtype, s_transtype_name, s_channel, s_channel_name, s_flowno
    local s_rspcode, s_rspmsg

    local s_wssn = ApmData.getField("request.http.header.$wssn", 1)
    if s_wssn ~= nil and s_wssn == "96368.hntobacco.com" then
        local s_wsra = ApmData.getField("request.http.header.$wsra", 1)
        if s_wsra ~= nil then
            ApmData.setSrcIp(s_wsra)
        end
    end

    s_title = ApmData.getField("transaction.title", 1)
--    logInfo("title:[%s]", s_title)

    if string.endswith(s_title, "/st/users/dologin/up") then
        s_transtype = "用户登录"
        s_transtype_name = "用户登录"
        local s_resp_body = ApmData.getField("response.http.body.1", 1)
        if s_resp_body ~= nil then
            local t_xml_tree = ApmXml.load(s_resp_body, "UTF-8")
            if t_xml_tree ~= nil then
                s_rspcode = ApmXml.getValue(t_xml_tree, "/xsm/@code")
                s_rspmsg = ApmXml.getValue(t_xml_tree, "/xsm/@msg")
            end
        end
    elseif string.endswith(s_title, "/ecweb/pm/cgtcartopt/addToCgtCart") then
        s_transtype = "添加到购物车"
        s_transtype_name = "添加到购物车"
        local s_resp_body = ApmData.getField("response.http.body.1", 1)
        if s_resp_body ~= nil then
            local t_kv = ApmHttpUtil.parseParams(s_resp_body)
            if t_kv ~= nil then
                s_rspcode = t_kv["rtn_code"]
                s_rspmsg = t_kv["msg"]
                s_flowno = t_kv["co_num"]
            end
        end
    elseif string.endswith(s_title, "/ecweb/pm/cgtcartopt/delFromCgtCart") then
        s_transtype = "从购物车删除"
        s_transtype_name = "从购物车删除"
        local s_resp_body = ApmData.getField("response.http.body.1", 1)
        if s_resp_body ~= nil then
            local t_kv = ApmHttpUtil.parseParams(s_resp_body)
            if t_kv ~= nil then
                s_rspcode = t_kv["rtn_code"]
                s_rspmsg = t_kv["msg"]
                s_flowno = t_kv["co_num"]
            end
        end
    elseif string.endswith(s_title, "/ecweb/pm/historyordopt/getHisOrderHead") then
        s_transtype = "查询历史订单"
        s_transtype_name = "查询历史订单"
        local s_resp_body = ApmData.getField("response.http.body.1", 1)
        if s_resp_body ~= nil then
            local t_kv = ApmHttpUtil.parseParams(s_resp_body)
            if t_kv ~= nil then
                s_rspcode = t_kv["rtn_code"]
                s_rspmsg = t_kv["msg"]
            end
        end
    elseif string.endswith(s_title, "/ecweb/pm/historyordopt/getHisOrderLine") then
        s_transtype = "查询指定订单"
        s_transtype_name = "查询指定订单"
        local t_kv = ApmHttpUtil.getParams(ApmData)
        if t_kv ~= nil then
            s_flowno = t_kv["co_num"]
        end
        local s_resp_body = ApmData.getField("response.http.body.1", 1)
        if s_resp_body ~= nil then
            local t_kv = ApmHttpUtil.parseParams(s_resp_body)
            if t_kv ~= nil then
                s_rspcode = t_kv["rtn_code"]
                s_rspmsg = t_kv["msg"]
            end
        end
    elseif string.endswith(s_title, "/ecweb/pm/cgtsupermarket/submitCo") then
--        logInfo("HTTP:["..KCYAN.."%s"..RESET.."]", ApmData.dumpFields(true))
        local t_kv = ApmHttpUtil.getParams(ApmData)
        local is_add = false
        if t_kv ~= nil then
            is_add = t_kv["is_add"] == "1"
            local s_resp_body = ApmData.getField("response.http.body.1", 1)
            if s_resp_body ~= nil then
                local t_kv = ApmHttpUtil.parseParams(s_resp_body)
                if t_kv ~= nil then
                    s_rspcode = t_kv["rtn_code"]
                    s_rspmsg = t_kv["msg"]
                    s_flowno = t_kv["co_num"]
                    if is_add then
                        s_transtype = "保存订单"
                        s_transtype_name = "保存订单"
                    else
                        s_transtype = "修改订单"
                        s_transtype_name = "修改订单"
                    end
                end
            end
        end
    elseif string.endswith(s_title, "/ecweb/order/cgtCoUpdate.htm") then
--        logInfo("HTTP:["..KGREEN.."%s"..RESET.."]", ApmData.dumpFields(true))
        local t_kv = ApmHttpUtil.getParams(ApmData)
        if t_kv ~= nil then
            local s_action = t_kv["action"]
            if s_action == "deleteCo" then
                s_transtype = "删除订单"
                s_transtype_name = "删除订单"
            elseif s_action == "submitCo" then
                s_transtype = "修改订单"
                s_transtype_name = "修改订单"
            end
        end
    elseif string.endswith(s_title, ".cmd") then
        s_transtype = s_title:match(".*/(.*)%.cmd")
        s_transtype_name = s_transtype
    elseif string.endswith(s_title, "elecPay.do") then
        s_transtype = "查询余额"
        s_transtype_name = "查询余额"
--        logInfo("HTTP:["..KGREEN.."%s"..RESET.."]", ApmData.dumpFields(true))
    else
--        logError("HTTP:["..KGREEN.."%s"..RESET.."]", ApmData.dumpFields(true))
    end

    if s_transtype ~= nil then
--        if s_rspcode == nil then
--            logInfo("HTTP:[%s]", ApmData.dumpFields(true))
--        else
            ApmData.replaceField("transaction.type_code", s_transtype)
            ApmData.replaceField("transaction.type_name", s_transtype_name)

            if s_channel ~= nil then
                ApmData.replaceField("transaction.channel_code", s_channel)
                ApmData.replaceField("transaction.channel_name", s_channel_name)
            end

            if s_flowno ~= nil then
                ApmData.replaceField("transaction.code", s_flowno)
            end

            if s_rspcode ~= nil then
                ApmData.replaceField("response.code_string", s_rspcode)
                ApmData.delFields("response.code")
            end

            if s_rspmsg ~= nil then
                ApmData.replaceField("response.error_message", s_rspmsg)
            end

            logInfo("http(%s:%d->%s:%d) name:%s|channel:%s|seq:%s|ret:%s|%s",
                ApmData.getSrcIp(),
                ApmData.getSrcPort(),
                parseIp(ApmData.getDstIp()),
                ApmData.getDstPort(),
                s_transtype_name,
                s_channel_name,
                s_flowno,
                s_rspcode,
                s_rspmsg
            )
--        end
    end

    return
end
--湖南烟草 - 统一订单系统 - HTTP
--v1.0.7
